package com.ghy.nba.controllers;

import com.ghy.nba.entities.BallStar;
import com.ghy.nba.entities.BallTeam;
import com.ghy.nba.services.BallTeamService;
import com.ghy.nba.services.BallstarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("ballstars")
public class BallStarControllers {


    @Autowired
    BallstarService ballstarService;

    @Autowired
    BallTeamService ballTeamService;
    @GetMapping
    public String disBallStars(Model model){
        List<BallStar>ballStars=ballstarService.getALL();
        model.addAttribute("ballstars",ballStars);
        return "ballstar/list-ballstar";

    }

    @GetMapping("/new")
    public String displayBallStarFrom(Model model){
        BallStar aballstar=new BallStar();
        List<BallTeam> ballTeams=ballTeamService.getALL();
        model.addAttribute("ballstar",aballstar);
        model.addAttribute("allBallteams",ballTeams);

        return "ballstar/new-ballstar";


    }
    @PostMapping("/save")
    public String createBallStar(Model model, @Valid BallStar ballStar, Errors errors){
        if (errors.hasErrors())
            return "redirect:/ballstars/new";

        ballstarService.save(ballStar);

        return "redirect:/ballstars";
    }

    @GetMapping("/update")
    public String displayBallStarUpdateForm(@RequestParam("id")long theId, Model model){

        BallStar theBallstar = ballstarService.findByBallstarId(theId);
        model.addAttribute("ballstar",theBallstar);
        return "ballstar/new-ballstar";
    }

    @GetMapping("/delete")
    public String deleteBallstar(@RequestParam("id")long theId, Model model){

        BallStar theBallstar = ballstarService.findByBallstarId(theId);
        ballstarService.delete(theBallstar);
        return "redirect:/ballstars";
    }

}
